package com.gz.http;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.Builder;
import java.net.http.HttpResponse;
import java.util.List;

import com.gz.bean.GZHttpBean;
import com.gz.bean.GZStaticString;
import com.gz.util.ImageUtil;

public class GZHttpUtil {
	
	
	public static GZHttpBean nikeHttpGet(String url,GZHttpBean gzHttpBean,String acceptType) throws Exception{
		
		HttpClient httpClient = gzHttpBean.getHttpClient();
		URI uri = new URI(url);
		Builder requestBuilder = HttpRequest.newBuilder(uri);
		String cookie = "MEIQIA_TRACK_ID=1TmJqWkWc9dfi9cs0maL7Xx0ItR; MEIQIA_VISIT_ID=1TmJqbD1fRJ9mdOQ3JjykgkYMIF; ";
		if(gzHttpBean.getAcw_tc()!=null && !"".equals(gzHttpBean.getAcw_tc())) {
			cookie += gzHttpBean.getAcw_tc()+";";
		}
		if(gzHttpBean.getSessionId()!=null && !"".equals(gzHttpBean.getSessionId())) {
			cookie += gzHttpBean.getSessionId()+";";
		}
		if(gzHttpBean.getUserMemberID()!=null && !"".equals(gzHttpBean.getUserMemberID())) {
			cookie += gzHttpBean.getUserMemberID()+";";
		}
		if(gzHttpBean.getXpShopMemberAuth()!=null && !"".equals(gzHttpBean.getXpShopMemberAuth())) {
			cookie += gzHttpBean.getXpShopMemberAuth()+";";
		}
		if(gzHttpBean.getServerID()!=null && !"".equals(gzHttpBean.getServerID())) {
			cookie += gzHttpBean.getServerID()+";";
		}
		if(gzHttpBean.getXpShop_CartID()!=null && !"".equals(gzHttpBean.getXpShop_CartID())) {
			System.out.println("---------"+gzHttpBean.getXpShop_CartID());
			cookie += gzHttpBean.getXpShop_CartID()+";";
		}
		if(gzHttpBean.getCn()!=null && !"".equals(gzHttpBean.getCn())) {
			cookie += gzHttpBean.getCn()+";";
		}
		if("JSON".equals(acceptType)) {
			requestBuilder.header("Accept",GZStaticString.ACCEPT_JSON);
		}else if("HTML".equals(acceptType)) {
			requestBuilder.header("Accept",GZStaticString.ACCEPT_HTML);
		}else if("JAVA".equals(acceptType)) {
			requestBuilder.header("Accept",GZStaticString.ACCEPT_GET_JAVA);
		}else if("IMAGE".equals(acceptType)) {
			requestBuilder.header("Accept",GZStaticString.ACCEPT_GET_IMAGE);
		}
		HttpRequest httpGetRequest = requestBuilder
    			.header("User-Agent",GZStaticString.USER_AGENT)
    			.header("Content-Type",GZStaticString.CONTENT_TYPE)
    			.header("accept-encoding", "")
    			.header("accept-language", GZStaticString.ACCEPT_LANGUAGE)
    			.header("cookie", cookie)
    			.header("referer", gzHttpBean.getRefererUrl())
    			.GET()
                .build();
		httpClient.sendAsync(httpGetRequest, HttpResponse.BodyHandlers.ofString())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
            	List<String> list = resp.headers().allValues("Set-Cookie");
	      	      for(String temp : list) {
		      	      	System.out.println(temp);
		      	      	if(temp.contains("XpShopMemberAuth")) {
		      	      		gzHttpBean.setXpShopMemberAuth(temp.split(";")[0]);
		      	      	}else if(temp.contains("userMemberID")) {
		      	      		gzHttpBean.setUserMemberID(temp.split(";")[0]);
		      	      	}else if(temp.contains("SERVERID")) {
		      	      		gzHttpBean.setServerID(temp.split(";")[0]);
		      	      	}else if(temp.contains("XpShop_CartID")) {
		      	      		gzHttpBean.setXpShop_CartID(temp.split(";")[0]);
		      	      	}else if(temp.contains("acw_tc")) {
		      	      		gzHttpBean.setAcw_tc(temp.split(";")[0]);
		      	      	}else if(temp.contains("ASP.NET_SessionId")) {
		      	      		gzHttpBean.setSessionId(temp.split(";")[0]);
		      	      	}else if(temp.contains("cn=")) {
		      	      		gzHttpBean.setCn(temp.split(";")[0]);
		      	      	}
		      	      }
                System.out.println(resp.version());
                System.out.println(resp.statusCode());
//                if(!resp.body().contains("<!DOCTYPE html>"))
                	System.out.println(resp.body());
                
                gzHttpBean.setReturnContent(resp.body());
            }
        }).join();
        
		return gzHttpBean;
	}
	public static GZHttpBean nikeHttpGetImage(String url,GZHttpBean gzHttpBean,String acceptType) throws Exception{
		
		HttpClient httpClient = gzHttpBean.getHttpClient();
		URI uri = new URI(url);
		Builder requestBuilder = HttpRequest.newBuilder(uri);
		String cookie = "MEIQIA_TRACK_ID=1TmJqWkWc9dfi9cs0maL7Xx0ItR; MEIQIA_VISIT_ID=1TmJqbD1fRJ9mdOQ3JjykgkYMIF; ";
		if(gzHttpBean.getAcw_tc()!=null && !"".equals(gzHttpBean.getAcw_tc())) {
			cookie += gzHttpBean.getAcw_tc()+";";
		}
		if(gzHttpBean.getSessionId()!=null && !"".equals(gzHttpBean.getSessionId())) {
			cookie += gzHttpBean.getSessionId()+";";
		}
		if(gzHttpBean.getUserMemberID()!=null && !"".equals(gzHttpBean.getUserMemberID())) {
			cookie += gzHttpBean.getUserMemberID()+";";
		}
		if(gzHttpBean.getXpShopMemberAuth()!=null && !"".equals(gzHttpBean.getXpShopMemberAuth())) {
			cookie += gzHttpBean.getXpShopMemberAuth()+";";
		}
		if(gzHttpBean.getServerID()!=null && !"".equals(gzHttpBean.getServerID())) {
			cookie += gzHttpBean.getServerID()+";";
		}
		if(gzHttpBean.getXpShop_CartID()!=null && !"".equals(gzHttpBean.getXpShop_CartID())) {
			cookie += gzHttpBean.getXpShop_CartID()+";";
		}
		if(gzHttpBean.getCn()!=null && !"".equals(gzHttpBean.getCn())) {
			cookie += gzHttpBean.getCn()+";";
		}
		if("JSON".equals(acceptType)) {
			requestBuilder.header("Accept",GZStaticString.ACCEPT_JSON);
		}else if("HTML".equals(acceptType)) {
			requestBuilder.header("Accept",GZStaticString.ACCEPT_HTML);
		}else if("JAVA".equals(acceptType)) {
			requestBuilder.header("Accept",GZStaticString.ACCEPT_GET_JAVA);
		}else if("IMAGE".equals(acceptType)) {
			requestBuilder.header("Accept",GZStaticString.ACCEPT_GET_IMAGE);
		}
		HttpRequest httpGetRequest = requestBuilder
    			.header("User-Agent",GZStaticString.USER_AGENT)
    			.header("Content-Type",GZStaticString.CONTENT_TYPE)
    			.header("accept-encoding", "")
    			.header("accept-language", GZStaticString.ACCEPT_LANGUAGE)
    			.header("cookie", cookie)
    			.header("referer", gzHttpBean.getRefererUrl())
    			.GET()
                .build();
		httpClient.sendAsync(httpGetRequest, HttpResponse.BodyHandlers.ofInputStream())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
            	List<String> list = resp.headers().allValues("Set-Cookie");
	      	      for(String temp : list) {
		      	      	System.out.println(temp);
		      	      	if(temp.contains("XpShopMemberAuth")) {
		      	      		gzHttpBean.setXpShopMemberAuth(temp.split(";")[0]);
		      	      	}else if(temp.contains("userMemberID")) {
		      	      		gzHttpBean.setUserMemberID(temp.split(";")[0]);
		      	      	}else if(temp.contains("SERVERID")) {
		      	      		gzHttpBean.setServerID(temp.split(";")[0]);
		      	      	}else if(temp.contains("XpShop_CartID")) {
		      	      		gzHttpBean.setXpShop_CartID(temp.split(";")[0]);
		      	      	}else if(temp.contains("acw_tc")) {
		      	      		gzHttpBean.setAcw_tc(temp.split(";")[0]);
		      	      	}else if(temp.contains("ASP.NET_SessionId")) {
		      	      		gzHttpBean.setSessionId(temp.split(";")[0]);
		      	      	}else if(temp.contains("cn=")) {
		      	      		gzHttpBean.setCn(temp.split(";")[0]);
		      	      	}
		      	      }
                System.out.println(resp.version());
                System.out.println(resp.statusCode());
                System.out.println(resp.body());
                try {
                	String result = ImageUtil.identifyImage(resp.body());
                	gzHttpBean.setReturnContent(result);
                } catch (Exception e) {
					e.printStackTrace();
				}   
                
//                try {
//	                OutputStream os = new FileOutputStream("c:/test.gif");
//	                int bytesRead = 0;
//	                byte[] buffer = new byte[8192];
//	                while ((bytesRead = resp.body().read(buffer, 0, 8192)) != -1) {
//	                   os.write(buffer, 0, bytesRead);
//	                }
//	                os.close();
//                } catch (Exception e) {
//					e.printStackTrace();
//				}      
//                resp.body().close();
//                gzHttpBean.setReturnContent(sb.toString());
            }
        }).join();
        
		return gzHttpBean;
	}
	public static GZHttpBean nikeHttpPost(String url,GZHttpBean gzHttpBean,String acceptType ) throws Exception{
		
		HttpClient httpClient = gzHttpBean.getHttpClient();
		URI uri = new URI(url);
		Builder requestBuilder = HttpRequest.newBuilder(uri);
		String cookie = "MEIQIA_TRACK_ID=1TmJqWkWc9dfi9cs0maL7Xx0ItR; MEIQIA_VISIT_ID=1TmJqbD1fRJ9mdOQ3JjykgkYMIF; ";
		if(gzHttpBean.getAcw_tc()!=null && !"".equals(gzHttpBean.getAcw_tc())) {
			cookie += gzHttpBean.getAcw_tc()+";";
		}
		if(gzHttpBean.getSessionId()!=null && !"".equals(gzHttpBean.getSessionId())) {
			cookie += gzHttpBean.getSessionId()+";";
		}
		if(gzHttpBean.getUserMemberID()!=null && !"".equals(gzHttpBean.getUserMemberID())) {
			cookie += gzHttpBean.getUserMemberID()+";";
		}
		if(gzHttpBean.getXpShopMemberAuth()!=null && !"".equals(gzHttpBean.getXpShopMemberAuth())) {
			cookie += gzHttpBean.getXpShopMemberAuth()+";";
		}
		if(gzHttpBean.getServerID()!=null && !"".equals(gzHttpBean.getServerID())) {
			cookie += gzHttpBean.getServerID()+";";
		}
		if(gzHttpBean.getXpShop_CartID()!=null && !"".equals(gzHttpBean.getXpShop_CartID())) {
			cookie += gzHttpBean.getXpShop_CartID()+";";
		}
		if(gzHttpBean.getCn()!=null && !"".equals(gzHttpBean.getCn())) {
			cookie += gzHttpBean.getCn()+";";
		}
		if("JSON".equals(acceptType)) {
			requestBuilder.header("Accept",GZStaticString.ACCEPT_JSON);
		}else if("HTML".equals(acceptType)) {
			requestBuilder.header("Accept",GZStaticString.ACCEPT_HTML);
		}
		HttpRequest httpGetRequest = requestBuilder
    			.header("User-Agent",GZStaticString.USER_AGENT)
    			.header("Content-Type",GZStaticString.CONTENT_TYPE)
    			.header("accept-encoding", "")
    			.header("accept-language", GZStaticString.ACCEPT_LANGUAGE)
    			.header("cookie", cookie)
    			.header("referer",gzHttpBean.getRefererUrl())
    			.POST(HttpRequest.BodyPublishers.ofString(gzHttpBean.getRequestBody()))
                .build();
		httpClient.sendAsync(httpGetRequest, HttpResponse.BodyHandlers.ofString())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
            	List<String> list = resp.headers().allValues("Set-Cookie");
	      	      for(String temp : list) {
	      	      	System.out.println(temp);
	      	      	if(temp.contains("XpShopMemberAuth")) {
	      	      		gzHttpBean.setXpShopMemberAuth(temp.split(";")[0]);
	      	      	}else if(temp.contains("userMemberID")) {
	      	      		gzHttpBean.setUserMemberID(temp.split(";")[0]);
	      	      	}else if(temp.contains("SERVERID")) {
	      	      		gzHttpBean.setServerID(temp.split(";")[0]);
	      	      	}else if(temp.contains("XpShop_CartID")) {
	      	      		gzHttpBean.setXpShop_CartID(temp.split(";")[0]);
	      	      	}else if(temp.contains("acw_tc")) {
	      	      		gzHttpBean.setAcw_tc(temp.split(";")[0]);
	      	      	}else if(temp.contains("ASP.NET_SessionId")) {
	      	      		gzHttpBean.setSessionId(temp.split(";")[0]);
	      	      	}else if(temp.contains("cn=")) {
	      	      		gzHttpBean.setCn(temp.split(";")[0]);
	      	      	}
	      	      }
                System.out.println(resp.version());
                System.out.println(resp.statusCode());
                System.out.println(resp.body());
                gzHttpBean.setReturnContent(resp.body());
            }
        }).join();
        
		return gzHttpBean;
	}
}
